package com.test.redis;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
	
	@Value("${redis.host}")
    private String host;
	@Value("${redis.port}")
    private int port;
	@Value("${redis.maxIdle}")
    private int maxIdle;
	@Value("${redis.minIdle}")
    private int minIdle;
	@Value("${redis.maxTotal}")
    private int maxTotal;
	@Value("${redis.maxWait}")
    private int maxWait;
	@Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;
	@Autowired
	private JedisPoolConfig jedisPoolConfig;//redis缓冲池配置
	@Autowired
	private JedisPool jedisPool;//redis缓冲池
	
	@Bean
	public JedisPoolConfig jedisPoolConfig(){
		JedisPoolConfig cfg = new JedisPoolConfig();
		cfg.setMaxTotal(maxTotal);
		cfg.setMinIdle(minIdle);
		cfg.setMaxIdle(maxIdle);
		cfg.setMaxWaitMillis(maxWait);
		cfg.setTestOnBorrow(testOnBorrow);
		return cfg;
	}
	
	@Bean
	public JedisPool jedisPool(){
		return new JedisPool(jedisPoolConfig, host, port);
	}
	
	@Bean
	public Jedis jedis(){
		return new Jedis(host, port);
	}

}
